package com.hgq.jssjyyxt.controller;

import com.hgq.jssjyyxt.entity.Bookings;
import com.hgq.jssjyyxt.entity.Courses;
import com.hgq.jssjyyxt.entity.Trainers;
import com.hgq.jssjyyxt.service.BookingsService;
import com.hgq.jssjyyxt.util.Result;

import com.hgq.jssjyyxt.util.ValidToken;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * (Bookings)表控制层
 *
 * @author makejava
 * @since 2024-04-11 13:53:35
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping("bookings")
public class BookingsController {
    /**
     * 服务对象
     */
    @Resource
    private BookingsService bookingsService;
    @Resource
    RedisTemplate<String, Integer> redisTemplate;

    /**
     * 分页查询
     *
     * @param bookings 筛选条件
     * @param limit    分页对象
     * @param page     分页对象
     * @return 查询结果
     */
    @GetMapping("findAll")
    public Result findAll(Bookings bookings, Integer limit, Integer page) {
        if (limit == null || page == null) {
            return Result.failure("limit和page不能为空");
        }
        return bookingsService.selectAll(bookings, limit, page);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("getById")
    public Result getById(Integer id) {
        return Result.success(this.bookingsService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param bookings 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public Result add(HttpServletRequest request,@RequestBody Bookings bookings) {
        //判断是否为空
        if (bookings == null) {
            return Result.failure("参数不能为空");
        }
        Integer id = ValidToken.getIdByToken(request, redisTemplate);
        bookings.setUserId(id);
        bookings.setBookingTime(new Date());
        return bookingsService.saveData(bookings);
    }
    /**
     * 编辑数据
     *
     * @param bookings 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    public Result edit(Bookings bookings) {
        //判断是否为空
        if (bookings == null) {
            return Result.failure("参数不能为空");
        }

        if (this.bookingsService.updateById(bookings)) {
            return Result.success("修改成功");
        }
        return Result.failure("修改失败");
    }

    /**
     * 编辑数据
     *
     * @param bookings 实体
     * @return 编辑结果
     */
    @PostMapping("editState")
    public Result editState(@RequestBody Bookings bookings) {
        //判断是否为空
        if (bookings == null) {
            return Result.failure("参数不能为空");
        }
        return bookingsService.editState(bookings);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("delete")
    public Result deleteById(Integer id) {
        //判断id是否为空
        if (id == null) {
            return Result.failure("id不能为空");
        }

        if (this.bookingsService.removeById(id)) {
            return Result.success("删除成功");
        }
        return Result.failure("删除失败");
    }

}

